package com.study.blog.controller;

import com.study.blog.common.result.BaseResponse;
import com.study.blog.model.dto.AdminLoginDTO;
import com.study.blog.model.dto.AdminRegisterDTO;
import com.study.blog.model.entity.Admin;
import com.study.blog.service.impl.AdminServiceImpl;
import com.study.blog.utils.CopyUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Api(tags = "用户管理")
@RestController
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private AdminServiceImpl adminService;


    @ApiOperation(value = "激活账户")
    @ApiImplicitParam(name = "email", value = "邮箱", required = true, example = "123456@qq.com")
    @GetMapping("/activation")
    public BaseResponse<String> activationAccount(@RequestParam("email") String email) {
        Boolean isValid = adminService.checkAccountValid(email);
        if (isValid) {
            return BaseResponse.error("账户已是激活状态");
        }

        Boolean isSuccess = adminService.activationAccount(email);
        if (!isSuccess) {
            return BaseResponse.error("激活失败");
        }
        return BaseResponse.success("激活成功");

    }


    @ApiOperation(value = "用户登录")
    @PostMapping("/login")
    public BaseResponse<String> login(@Validated @RequestBody AdminLoginDTO adminDTO) {

        Boolean isExist = adminService.checkAccountExist(adminDTO.getEmail());

        if (isExist) {
            // 说明邮箱存在, 然后判断邮箱是否激活成功
            Boolean isValid = adminService.checkAccountValid(adminDTO.getEmail());

            if (!isValid) {
                return BaseResponse.error("邮箱还没激活");
            }

            Boolean isLogin = adminService.login(adminDTO.getEmail(), adminDTO.getPassword());
            if (!isLogin) {
                return BaseResponse.error("邮箱或密码错误");
            }

            return BaseResponse.success("登录成功");
        }
        return BaseResponse.error("账户不存在");
    }


    @ApiOperation(value = "注册用户")
    @PostMapping("/register")
    public BaseResponse<String> register(@RequestBody @Validated AdminRegisterDTO registerDTO) {
        System.out.println(registerDTO);
        Admin admin = CopyUtil.copy(registerDTO, Admin.class);
        Boolean isExist = adminService.checkAccountExist(admin.getEmail());
        if (isExist) {
            return BaseResponse.error("邮箱已存在");
        }

        Boolean isRegister = adminService.register(admin);
        if (!isRegister) {
            return BaseResponse.error("注册失败");
        }

        return BaseResponse.success("注册成功, 请去邮箱激活账号");
    }

    @ApiOperation(value = "注销用户")
    @GetMapping("/abort")
    @ApiImplicitParam(name = "email", value = "邮箱", required = true, example = "123456@qq.com")
    public BaseResponse<String> abort(@RequestParam("email") String email) {
        Boolean isExist = adminService.checkAccountExist(email);
        if (!isExist) {
            return BaseResponse.error("此用户不存在");
        }

        final Boolean isAbort = adminService.abort(email);
        if (!isAbort) {
            return BaseResponse.error("注销失败");
        }
        return BaseResponse.success("注销成功");
    }
}
